package com.zhou.core.logana.handle;

import com.zhou.core.logana.LogAnaResult;
import com.zhou.core.logana.LogRecord;
import com.zhou.util.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by zhouyongjun on 2020/6/12
 */
public class LogAnaHandlerProxy {
    Class<? extends ILogAnaHandler> recordClass;
    protected  long nextRecordDateTime;//��ȷ����
    int tickPeriodSeconds = 60;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    public Class<? extends ILogAnaHandler> getRecordClass() {
        return recordClass;
    }

    public void setRecordClass(Class<? extends ILogAnaHandler> recordClass) {
        this.recordClass = recordClass;
    }

    public long getNextRecordDateTime() {
        return nextRecordDateTime;
    }

    public void setNextRecordDateTime(long nextRecordDateTime) {
        this.nextRecordDateTime = nextRecordDateTime;
    }

    public int getTickPeriodSeconds() {
        return tickPeriodSeconds;
    }

    public void setTickPeriodSeconds(int tickPeriodSeconds) {
        this.tickPeriodSeconds = tickPeriodSeconds;
    }

    public LogRecord tick(Object... param) throws IllegalAccessException, InstantiationException {
        LogAnaResult result = checkPeriodSecondFulfill();
        if(result.isFail()) return null;
        logger.info("LogAnaHandlerProxy  run ... clazz="+recordClass.getName());
        ILogAnaHandler  handler  = recordClass.newInstance();
        logger.debug(">>>>> handler ["+recordClass.getName()+"] tick begin: " + TimeUtils.formatNoSpr(TimeUtils.now()));
        LogRecord logRecord  = handler.handle(param);
        logger.debug(">>>>> tick end: " + TimeUtils.formatNoSpr(TimeUtils.now()));
         return logRecord;
    }

    private LogAnaResult checkPeriodSecondFulfill() {
        long now = TimeUtils.nowInteger();
        return nextRecordDateTime > now ? LogAnaResult.RESULT_FAIL : LogAnaResult.RESULT_SUCC;
    }
}
